package com.bwie.bwedu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.bean.Agency;
import com.bwie.bwedu.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by HP- on 2016/7/25.
 */
public class MyAdapter  extends BaseAdapter implements Filterable{
    private List<Agency> list;
    private Context context;
    private ArrayList<Agency> mUnfilteredData;
    private ArrayFilter mfilter;
    private OnSelectItemListener listener;

    public void setListener(OnSelectItemListener listener) {
        this.listener = listener;
    }

    public interface OnSelectItemListener{
        void onSelectItem(Agency agency);
    }

    public MyAdapter(Context context, List<Agency> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_daili,null);
            holder.item_tv = (TextView) convertView.findViewById(R.id.daili_tv);
            holder.item_tv.setFocusable(false);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
//        holder.item_tv.setText(list.get(position).getDname()+"\n"+list.get(position).getDid());
        holder.item_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("====success======="+list.get(position).DLRID);
                listener.onSelectItem(list.get(position));
            }
        });
        holder.item_tv.setText(list.get(position).DLRXM+"\n"+list.get(position).DWMC);
        return convertView;
    }


    static class ViewHolder{
        TextView item_tv;
    }

    @Override
    public Filter getFilter() {
        if(mfilter == null){
            mfilter = new ArrayFilter();
        }
        return mfilter;
    }

    private class ArrayFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if(mUnfilteredData == null){
                mUnfilteredData =  new ArrayList<Agency>(list);
            }

            if(constraint == null||constraint.length() == 0){
                ArrayList<Agency> mlist =  mUnfilteredData;
                results.values = list;
                results.count = list.size();
            }else{
                String preFix = constraint.toString().toLowerCase();
                ArrayList<Agency> unfilteredValues  = mUnfilteredData;
                int count = unfilteredValues.size();
                ArrayList<Agency> newValue = new ArrayList<>(count);
                for (int i = 0; i < count; i++) {
                    Agency dlinfo = unfilteredValues.get(i);
                    if(dlinfo!=null){

//                        if(dlinfo.getDname()!=null && dlinfo.getDname().startsWith(preFix)){
                        if(dlinfo.DLRXM!=null && dlinfo.DLRXM.startsWith(preFix)){

                            newValue.add(dlinfo);
                        }else if(dlinfo.DWMC!=null && dlinfo.DWMC.startsWith(preFix)){

                            newValue.add(dlinfo);
                        }
                    }
                }
                results.values = newValue;
                results.count = newValue.size();
            }
            return results;
        }


        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            list = (ArrayList<Agency>)results.values;
            if(results.count>0){
                notifyDataSetChanged();
            }else{
                notifyDataSetInvalidated();
            }
        }
    }

}
